<?php

namespace app\admin\controller;

use app\models\{Banner, Shoplist, Cate, Goods, Helps, Withdrawal, Admin, ShopLog};
use think\Db;
use think\Validate;

class System extends Base
{
    public function banner()
    {
        $model = new Banner();
        $shopmodel = new Shoplist();
        $data = $this->request->get();
        $results = $model->order('sorts desc');
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['shopid'])) {
            $results = $results->where('shopid', $data['shopid']);
        }
        $admin = $this->request->admin;
        if ($admin['type'] == 2) {
            $shopid = $shopmodel->where(['area' => $admin['area']])->column('id');
            if (count($shopid) > 0) {
                $results = $results->where(['shopid' => $shopid]);
            } else {
                $result = $results->where(['shopid' => 0]);
            }
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            $shopid = $shopmodel->where(['street' => $admin['street']])->column('id');
            if (count($shopid) > 0) {
                $results = $results->where(['shopid' => $shopid]);
            } else {
                $result = $results->where(['shopid' => 0]);
            }
        }
        $shoplist = $shopmodel->field('id,title,account')->select();
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['shopname'] = $shopmodel->where('id', $result['shopid'])->value('title');
        }
        return view(null, [
            'shoplist' => $shoplist,
            'list' => $results
        ]);
    }

    public function banner_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'shopid' => 'require',
                'banner' => 'require',
            ], [
                'title.require' => '活动名称不能为空',
                'shopid.require' => '商家不能为空',
                'banner.require' => '活动图片不能为空',
            ]);
            if (!$validate->check($data)) {
                $this->result([], 1, $validate->getError());
            }
            if (empty($data['type'])) {
                $data['getid'] = 0;
            }
            if ($data['type'] == 1 || $data['type'] == 2) {
                if (empty($data['getid'])) {
                    return json(['code' => 1, 'data' => [], 'msg' => '跳转商品|品类不能为空']);
                }
                //查找商品是否存在
                if ($data['type'] == 1) {
                    $model = new Goods();
                    $goods = $model->where(['id' => $data['getid'], 'shopid' => $data['shopid'], 'isdel' => 1])->find();
                    if (empty($goods)) {
                        return json(['code' => 1, 'data' => [], 'msg' => '商品不存在']);
                    }
                    if ($goods['isSale'] == 2) {
                        return json(['code' => 1, 'data' => [], 'msg' => '商品已下架，请上架后再操作']);
                    }
                }
                //查找商品是否存在
                if ($data['type'] == 2) {
                    $model = new Cate();
                    $cate = $model->where(['id' => $data['getid'], 'shopid' => $data['shopid']])->find();
                    if (empty($cate)) {
                        return json(['code' => 1, 'data' => [], 'msg' => '分类不存在']);
                    }
                }
            }
            $bannermodel = new Banner();
            if ($data['types'] == 2 && empty($data['id'])) {
                $count = $bannermodel->where(['shopid' => $data['shopid'], 'types' => 2])->find();
                if (!empty($count)) {
                    return json(['code' => 1, 'data' => [], 'msg' => '快捷入口不支持多图片上传']);
                }
            }
            if (!empty($data['id'])) {
                $res = $bannermodel->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $bannermodel->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Banner();
            $shopmodel = new Shoplist();
            $catemodel = new Cate();
            $goodsmodel = new Goods();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            $shoplist = $shopmodel->field('id,title,account')->select();
            if ($results) {
                $results['shopname'] = $shopmodel->where('id', $results['shopid'])->value('title');
                if ($results['type'] == 1) {
                    $results['list'] =  $goodsmodel->where(['shopid' => $results['shopid']])->field('id,title')->select();
                }
                if ($results['type'] == 2) {
                    $results['list'] =  $catemodel->where(['shopid' => $results['shopid']])->field('id,name as title')->select();
                }
            }
            return view(null, [
                'row' => $results,
                'shoplist' => $shoplist

            ]);
        }
    }

    public function banner_del()
    {
        $id = trim(input('id'), ',');
        $model = new Banner();
        $res = $model->where(['id' => $id])->delete();
        if ($res) {
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    public function cate()
    {
        $model = new Cate();
        $shopmodel = new Shoplist();
        $data = $this->request->get();
        $results = $model->order(['ishow' => 'asc', 'sorts' => 'desc']);
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['shopid'])) {
            $results = $results->where('shopid', $data['shopid']);
        }
        $shoplist = $shopmodel->field('id,title,account')->select();
        $admin = $this->request->admin;
        if ($admin['type'] == 2) {
            $shopid = $shopmodel->where(['area' => $admin['area']])->column('id');
            if (count($shopid) > 0) {
                $results = $results->where(['shopid' => $shopid]);
            } else {
                $result = $results->where(['shopid' => 0]);
            }
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            $shopid = $shopmodel->where(['street' => $admin['street']])->column('id');
            if (count($shopid) > 0) {
                $results = $results->where(['shopid' => $shopid]);
            } else {
                $result = $results->where(['shopid' => 0]);
            }
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['shopname'] = $shopmodel->where('id', $result['shopid'])->value('title');
        }
        return view(null, [
            'shoplist' => $shoplist,
            'list' => $results
        ]);
    }

    public function cate_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'shopid' => 'require',
                'remark' => 'require',
            ], [
                'name.require' => '分类名称不能为空',
                'remark.require' => '分类备注不能为空',
                'shopid.require' => '商家不能为空',
            ]);
            if (!$validate->check($data)) {
                $this->result([], 1, $validate->getError());
            }
            $bannermodel = new Cate();
            if (!empty($data['id'])) {
                $res = $bannermodel->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $bannermodel->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Cate();
            $shopmodel = new Shoplist();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            $shoplist = $shopmodel->field('id,title,account')->select();
            if ($results) {
                $results['shopname'] = $shopmodel->where('id', $results['shopid'])->value('title');
            }
            return view(null, [
                'row' => $results,
                'shoplist' => $shoplist
            ]);
        }
    }

    public function helps()
    {
        $model = new Helps();
        $data = $this->request->get();
        $results = $model->order('id desc');
        if (!empty($data['title'])) {
            $results = $results->where('title', 'like', '%' . $data['title'] . '%');
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, [
            'list' => $results
        ]);
    }

    public function helps_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'content' => 'require',
            ], [
                'title.require' => '标题不能为空',
                'shopid.require' => '内容不能为空',
            ]);
            if (!$validate->check($data)) {
                $this->result([], 1, $validate->getError());
            }
            $bannermodel = new Helps();
            if (!empty($data['id'])) {
                $res = $bannermodel->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $bannermodel->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Helps();
            $id = $this->request->get('id/d', 0);
            $results = $model->where('id', $id)->find();
            return view(null, [
                'row' => $results
            ]);
        }
    }

    public function helps_del()
    {
        $id = trim(input('id'), ',');
        $model = new Helps();
        $res = $model->where(['id' => $id])->delete();
        if ($res) {
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }


    public function widthdraw()
    {
        $model = new Withdrawal();
        $shopmodel = new Shoplist();
        $data = $this->request->get();
        $result = $model->order('id desc');
        if (!empty($data['status'])) {
            $result = $result->where('status', $data['status']);
        }
        if (!empty($data['shopid'])) {
            $result = $result->where('shopid', $data['shopid']);
        }
        if (!empty($data['title'])) {
            $result = $result->where('name|cards', 'like', '%' . $data['title'] . '%');
        }
        $result = $result->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($result as $results) {
            if ($results['atype'] == 1) {
                $results['shopname'] = $shopmodel->where('id', $results['shopid'])->value('title');
            } else {
                $results['shopname'] = (new Admin())->where('id', $results['shopid'])->value('nickname');
            }
        }
        $shoplist = $shopmodel->field('id,title,account')->select();
        return view(null, [
            'list' => $result,
            'shoplist' => $shoplist
        ]);
    }

    public function widthdrawstr()
    {
        $status = $this->request->post('status/d', 0);
        $id = $this->request->post('id/d', 0);
        if (empty($id) || empty($status)) {
            return json(['code' => 1, 'data' => [], 'msg' => '参数错误，请重试']);
        }
        if ($this->request->admin['type'] != 1) {
            return json(['code' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        $model = new Withdrawal();
        $shoplogmodel = new ShopLog();
        $shopmodel = new Shoplist();
        $result = $model->where(['id' => $id, 'status' => 1])->find();
        if (empty($result)) {
            return json(['code' => 1, 'data' => [], 'msg' => '信息已审核或不存在']);
        }
        if ($status == 2) {
            $res = $model->where(['id' => $id, 'status' => 1])->update(['status' => 2]);
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
        }
        if ($status == 3) {
            $shop = $shopmodel->where('id', $result['shopid'])->find();
            if (empty($shop)) {
                return json(['code' => 1, 'data' => [], 'msg' => '商户信息不存在']);
            }
            Db::startTrans();
            try {
                $model->where(['id' => $id, 'status' => 1])->update(['status' => 3]);
                //返回商户余额
                if ($result['atype'] == 1) {
                    $shoplogmodel->MoneyEdit($shop, abs($result['money']), 3, '打款驳回,管理员ID:' . $this->request->admin['id'], $result['id']);
                } else {
                    (new Admin())->MoneyEdit($shop, abs($result['money']), 3, '打款驳回,管理员ID:' . $this->request->admin['id'], $result['id']);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 1, 'data' => [], 'msg' => $e->getMessage()]);
            }
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }
}
